import 'package:flutter_app/class/MyApp10.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:intl/intl.dart';

class MyApp11 extends StatelessWidget {
  const MyApp11({super.key});

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return MaterialApp(
      /*本地化配置*/
      localizationsDelegates: const [
        GlobalWidgetsLocalizations.delegate,
        GlobalMaterialLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate
      ],
      supportedLocales: const [
        Locale('zh','CH'),
        Locale('en','US')
      ],
      locale: const Locale('zh','CH'),
      theme: ThemeData(primarySwatch: Colors.deepPurple),
      home: const MyApp11State(),
    );
  }
}

class MyApp11State extends StatefulWidget {
  const MyApp11State({super.key});

  @override
  State<MyApp11State> createState() => _MyApp11State();
}


class _MyApp11State extends State<MyApp11State> {

  late var _isEmail = null;
  final myController = TextEditingController();
  final myController2 = TextEditingController();
  final myController3 = TextEditingController();
  final myController4 = TextEditingController();

  /*正则验证*/
  bool checkIsEmail(String em) {
    String emailRegexp =
        r'^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$';

    RegExp regExp = RegExp(emailRegexp);
    return regExp.hasMatch(em);
  }

  /*显示日期*/
  _showDatePicker() async {
    var dateTime = DateTime.now();

    //方法是Future方法，点击日期选择控件的确定按钮后，返回选择的日期
    var date = await showDatePicker(context: context,
        initialDate: dateTime,//初始化时间，通常情况下设置为当前时间。
        firstDate: DateTime(1990),
        lastDate: DateTime(2050),
        locale: const Locale('zh')
    );

    if(date == null) return;
    setState(() {
      dateTime = date;
      var format = DateFormat('yyyy-MM-dd HH:mm:ss');
      myController4.text = format.format(dateTime);
    });
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: const Text('Form表单'),
        actions: [
          IconButton(
              icon: const Icon(Icons.chevron_right),
              onPressed: (){
                  Navigator.push(context, MaterialPageRoute(builder: (context) => const MyApp10()));
              })
        ],
      ),
      body: Padding(
        padding: const EdgeInsets.all(16),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            TextField(
              autofocus:true,
              keyboardType:TextInputType.emailAddress,//指定输入类型
              decoration: InputDecoration(
                  labelText: "请输入邮箱",
                  helperText: _isEmail == null ? "回车提交验证邮箱" : (_isEmail == true ? "邮箱验证通过":null),
                  errorText: _isEmail == null ? null :  (_isEmail != true ? "邮箱格式有误":null),
                  suffixText: '',
                  suffixIcon: const Icon(Icons.backspace),
                  prefixText: '邮箱：',
                  prefixIcon: const Icon(Icons.email),
              ),
              maxLength: 30,
              maxLines: 1,
              controller: myController,
              onSubmitted: (txt){
                setState(() {
                    _isEmail = checkIsEmail(txt);
                    debugPrint(txt);
                });
              },
            ),
            TextField(
              autofocus:true,
              keyboardType:TextInputType.datetime,//指定输入类型
              decoration: const InputDecoration(
                labelText: "请选择日期",
                helperText: "这里日期文本框",
                suffixText: '日期不为空',
                prefixIcon: Icon(Icons.date_range),
                prefixText: '日期：',
              ),
              maxLength: 30,
              maxLines: 1,
              controller: myController4,
              onTap: (){
                _showDatePicker();
              },
            ),
            Container(
              padding: const EdgeInsets.all(5.0),
              alignment: Alignment.topCenter,
              height: 100.0,
              decoration: BoxDecoration(
                  color: Colors.white,
                  border: Border.all(color: Colors.black12, width: 1.0),
                  borderRadius: BorderRadius.circular(12.0)),
                  child: TextFormField(
                    controller: myController2,
                    maxLines: 3,
                    maxLength: 100,
                    decoration: const InputDecoration.collapsed(hintText: '这里是多行文本框'),
                  ),
            ),
            TextField(
              obscureText: true,//是否是密码
              keyboardType:TextInputType.visiblePassword,//指定输入类型
              decoration: const InputDecoration(helperText: "这里是密码框",errorText: '这是错误提示信息'),
              maxLength: 18,
              controller: myController3,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        tooltip: '显示值',
        child: const Icon(Icons.text_fields),
        onPressed: (){
          showDialog(
              context: context,
              builder: (context){
                 return AlertDialog(
                   content: Text(myController.text),
                 );
              });
        },
      ),
    );
  }
}
